//
//  SearchFriendOrSessionViewController.m
//  Students affairs
//
//  Created by eliot on 16/5/9.
//  Copyright © 2016年 aleven. All rights reserved.
//

#import "SearchFriendOrSessionViewController.h"
#import "UIImageView+WebCache.h"

@interface SearchFriendOrSessionViewController ()<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

/** 好友 数据*/
@property(nonatomic,retain)NSMutableArray *friendsArr;
/** 会话数据*/
@property(nonatomic,retain)NSMutableArray *sessionArr;

@property(nonatomic,retain)NSMutableArray *searchResults;//接收数据源结果
@property (nonatomic, strong) UISearchBar *searchBar;


@property (nonatomic, strong) UIView *titleView;

@end

@implementation SearchFriendOrSessionViewController


//懒加载
- (NSMutableArray *)friendsArr{
    if (!_friendsArr) {
        _friendsArr = [NSMutableArray array];
    }
    return _friendsArr;
}

//懒加载
- (NSMutableArray *)sessionArr{
    if (!_sessionArr) {
        _sessionArr = [NSMutableArray array];
    }
    return _sessionArr;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage=[[UIImage alloc] init];
    //设置导航栏字体
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:38/255.0 green:94/255.0 blue:215/255.0 alpha:1],NSForegroundColorAttributeName, nil]];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    //设置导航bar颜色
    [self.navigationController.navigationBar setBackgroundColor:[UIColor grayColor]];

    [self.searchBar becomeFirstResponder];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [self.titleView endEditing:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.hidden = YES;
    [self.tableView registerNib:[UINib nibWithNibName:@"SessionCell" bundle:nil] forCellReuseIdentifier:@"SessionCell"];
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self settttttttt];
    [self.searchBar becomeFirstResponder];
    NSLog(@"123");
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [self.searchBar becomeFirstResponder];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)settttttttt {
    
    self.titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Width, 35)];//allocate titleView
    UIColor *color =  self.navigationController.navigationBar.barTintColor;
    [self.titleView setBackgroundColor:color];
    self.searchBar = [[UISearchBar alloc] init];
    self.searchBar.delegate = self;
    self.searchBar.frame = CGRectMake(0, 0, Width - 45, 35);
    self.searchBar.backgroundColor = color;
    self.searchBar.layer.cornerRadius = 8;
    self.searchBar.layer.masksToBounds = YES;
    [self.searchBar.layer setBorderWidth:8];
    [self.searchBar.layer setBorderColor:[UIColor whiteColor].CGColor];  //设置边框为白色
    self.searchBar.placeholder = @"搜索会话|好友";
    [self.titleView addSubview:self.searchBar];
    self.searchBar.delegate = self;
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.titleView addSubview:btn];
    
    btn.frame = CGRectMake(Width - 45, 0, 35, 35);
    [btn setImage:[UIImage imageNamed:@"btn_clear_40x40"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clearBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.navigationItem.titleView sizeToFit];
    self.navigationItem.titleView = self.titleView;

    [self.navigationItem setHidesBackButton:YES];
    self. searchBar.tintColor=[UIColor blueColor];
}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage=[[UIImage alloc] init];
    //设置导航栏字体
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:38/255.0 green:94/255.0 blue:215/255.0 alpha:1],NSForegroundColorAttributeName, nil]];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    //设置导航bar颜色
    [self.navigationController.navigationBar setBackgroundColor:[UIColor whiteColor]];
    
}

- (void)clearBtnClick {
    
    if ([self.searchBar.text isEqualToString:@""]) {
        [self.self.titleView endEditing:YES];
        [self.navigationController popViewControllerAnimated:YES];
    }else {
        self.searchBar.text = @"";
    }

}

//引入UISearchController之后, UITableView的内容也要做相应地变动: 即cell中要呈现的内容是items, 还是searchResults.这一点, 可以通过UISearchController的active属性来判断, 即判断输入框是否处于active状态.

//UITableView相关的很多方法都要根据active来做判断:


//配置导航条

//搜索条 取消按钮
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    [self.searchBar resignFirstResponder];
    self.tableView.hidden = YES;
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark 协议中的方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    
    [self.searchResults removeAllObjects];
    
    


}

#pragma mark - 收到通知

- (void)searchFriendsOrSession:(NSNotification *)noti {
    if ([noti.userInfo[@"requestStatus"] intValue] == 22) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:@"请求超时，请检查网络"];
            
        });
    }else {
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            if (![noti.userInfo[@"shops"] isKindOfClass:[NSString class]]) {
                
                self.sessionArr = [NSMutableArray arrayWithArray:noti.userInfo[@"shops"]];
            }
            if (![noti.userInfo[@"members"] isKindOfClass:[NSString class]]) {
                self.friendsArr = [NSMutableArray arrayWithArray:noti.userInfo[@"members"]];
            }
            
            
            //NSPredicate 谓词
            //        NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"self contains[cd] %@",self.searchController.searchBar.text];
            //        self.searchResults = [[self.items filteredArrayUsingPredicate:searchPredicate]mutableCopy];
            self.tableView.hidden = NO;
            //刷新表格
            [self.tableView reloadData];
        });

    }
    
}

#pragma mark - Table view data source



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        
        return self.friendsArr.count;
    }else {
        
        return self.sessionArr.count;
    }
    
}

//返回cell:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    [self.searchBar resignFirstResponder];
    [self.view endEditing:YES];
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    
    
    return nil;
}

//业务代理返回高:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
      
        return @"好友";
    }else {
    
        return @"会话";
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    


}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
